package com.ruoyi.flowable.controller;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.flowable.domain.WfDeployForm;
import com.ruoyi.flowable.domain.WfForm;
import com.ruoyi.flowable.domain.query.ProcessQuery;
import com.ruoyi.flowable.service.IWfDeployFormService;
import com.ruoyi.flowable.service.IWfDeployService;
import com.ruoyi.flowable.service.IWfFormService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

/**
 * 流程部署控制类
 *
 * @author siashan
 * @date 2023-01-06
 */
@Slf4j
@RestController
@RequestMapping("/flowable/deploy")
public class WfDeployController extends BaseController {

    @Autowired
    private  IWfDeployService deployService;
    @Autowired
    private  IWfDeployFormService deployFormService;
    @Autowired
    private IWfFormService wfFormService;

    /**
     * 查询流程部署列表
     */
    @PreAuthorize("@ss.hasPermi('flowable:deploy:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProcessQuery processQuery) {
        return deployService.queryPageList(processQuery);
    }

    /**
     * 查询流程部署版本列表
     */
    @PreAuthorize("@ss.hasPermi('flowable:deploy:list')")
    @GetMapping("/publishList")
    public TableDataInfo publishList(@RequestParam String processKey) {
        return deployService.queryPublishList(processKey);
    }

    /**
     * 激活或挂起流程
     *
     * @param state 状态（active:激活 suspended:挂起）
     * @param definitionId 流程定义ID
     */
    @PreAuthorize("@ss.hasPermi('flowable:deploy:state')")
    @PutMapping(value = "/changeState")
    public R<Void> changeState(@RequestParam String state, @RequestParam String definitionId) {
        deployService.updateState(definitionId, state);
        return R.ok();
    }

    /**
     * 读取xml文件
     * @param definitionId 流程定义ID
     * @return
     */
    @PreAuthorize("@ss.hasPermi('flowable:deploy:query')")
    @GetMapping("/bpmnXml/{definitionId}")
    public AjaxResult getBpmnXml(@PathVariable(value = "definitionId") String definitionId) {
        return AjaxResult.success(null, deployService.queryBpmnXmlById(definitionId));
    }

    /**
     * 删除流程模型
     * @param deployIds 流程部署ids
     */
    @PreAuthorize("@ss.hasPermi('flowable:deploy:remove')")
    @Log(title = "删除流程部署", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deployIds}")
    public R<String> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] deployIds) {
        deployService.deleteByIds(Arrays.asList(deployIds));
        return R.ok();
    }

    /**
     * 查询流程部署关联表单信息
     *
     * @param deployId 流程部署id
     */
    @GetMapping("/form/{deployId}")
    public R<?> start(@PathVariable(value = "deployId") String deployId) {
        WfDeployForm deployForm = deployFormService.getById(deployId);
        WfForm wfForm = wfFormService.getById(deployForm.getFormKey());
        if (Objects.isNull(wfForm)) {
            return R.fail("请先配置流程表单");
        }
        return R.ok(JSON.parseObject(wfForm.getContent(), Map.class));
    }
}
